package com.tuo51.manager.controller;


import com.tuo51.childcare.domain.vo.PTestItemVO;
import com.tuo51.childcare.service.PTestService;
import com.tuo51.foudation.model.MemVerificationCache;
import com.tuo51.foudation.model.StatusResponse;
import com.tuo51.foudation.query.base.PagerQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import static com.tuo51.foudation.constant.RequestConstant.TICKET;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

@RestController
@RequestMapping("/mapi/ptest")
@Api(value = "/mapi/ptest", tags = "OP - 知识点/趣味问答")
public class MAPIPtestController {

    @Autowired
    PTestService pTestService;

    @Autowired
    MemVerificationCache cache;

    @RequestMapping(value = "/list", method = GET)
    @ApiOperation(value = "资源列表")
    public StatusResponse list(@RequestHeader(TICKET) String ticket,
                               @RequestParam(value = "pageNum", required = false, defaultValue = "0")Integer pageNum,
                               @RequestParam(value = "pageSize", required = false, defaultValue = "15")Integer pageSize,
                               String categoryId, Integer type, Integer month, Integer forDay,
                               @RequestParam(value = "searchBy", required = false) String searchBy){

        cache.getStaffId(ticket);

        return StatusResponse.success(pTestService.getTestItem(categoryId, type, month, forDay, searchBy, PagerQuery.of(pageNum, pageSize)));
    }

    @RequestMapping(value = "/info", method = GET)
    @ApiOperation(value = "内容详情")
    public StatusResponse info(@RequestHeader(TICKET) String ticket,
                                   @RequestParam(value = "userId", required = true)String itemId){

        cache.getStaffId(ticket);

        return StatusResponse.success(pTestService.getTestItem(itemId));
    }

    @RequestMapping(value = "/edit", method = POST)
    @ApiOperation(value = "编辑，type=0 为问答， type=1 是知识点。  answerType, 1.单选， 2.多选，  3.文本， 4.判断")
    public StatusResponse edit(@RequestHeader(TICKET) String ticket,
                                    PTestItemVO item){
        cache.getStaffId(ticket);

        return StatusResponse.success(pTestService.editItem(item));
    }

    @RequestMapping(value = "/delete", method = POST)
    @ApiOperation(value = "删除")
    public StatusResponse delete(@RequestHeader(TICKET) String ticket,
                                    String itemId){
        cache.getStaffId(ticket);

        return StatusResponse.success(pTestService.deleteItem(itemId));
    }
}
